package com.example.tests;

import static org.testng.Assert.assertEquals;

import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.testng.annotations.Test;

public class PersonModificationTests extends TestBase{
	
	@Test(dataProvider = "randomValidPersonGenerator")
	public void modifySomePerson(PersonData person){
		app.getNavigationHelper().openMainPage();
	    
	    // save old state
	    List<PersonData> oldList = app.getPersonHelper().getPersons();
	    
	    Random rnd = new Random();
		int index = rnd.nextInt(oldList.size()-1);
	    
	    //actions   
		app.getPersonHelper().initPersonEditing(index);
		app.getPersonHelper().fillAddNewPersonPage(person);
		app.getPersonHelper().submitPersonModification();
		app.getPersonHelper().returnToHomePage();
		  
		//save new state
		List<PersonData> newList = app.getPersonHelper().getPersons();
  
		//compare states
		oldList.remove(index);
		oldList.add(person);
		Collections.sort(oldList);
		assertEquals(newList, oldList);	
	}
	
	@Test(dataProvider = "randomValidPersonGenerator")
	public void alsoModifySomePerson(PersonData person){
		app.getNavigationHelper().openMainPage();
	    
	    // save old state
	    List<PersonData> oldList = app.getPersonHelper().getPersons();
	    
	    Random rnd = new Random();
		int index = rnd.nextInt(oldList.size()-1);
	    
	    //actions   
		app.getPersonHelper().initPersonView(index);
		app.getPersonHelper().initPersonModification();
		app.getPersonHelper().fillAddNewPersonPage(person);
		app.getPersonHelper().submitPersonModification();
		app.getPersonHelper().returnToHomePage();
		  
		//save new state
		List<PersonData> newList = app.getPersonHelper().getPersons();

		//compare states
		oldList.remove(index);
		oldList.add(person);
		Collections.sort(oldList);
		assertEquals(newList, oldList);	
	}
	
}
